/**
 * Created by loso on 2017/3/17.
 */
public class _541_ReverseStringII {

    public static void main(String[] args) {
        _541_ReverseStringII a = new _541_ReverseStringII();
        System.out.println(a.reverseStr("abcdefg",2));
    }

    public String reverseStr(String s, int k) {
        StringBuilder stringBuilder  = new StringBuilder(s);
        if (k>=s.length()){
            return stringBuilder.reverse().toString();
        }
        int i;
        for (i = 0; i < s.length(); i=i+2*k) {
            if (i+k>s.length()){
                stringBuilder.replace(i,s.length(),new StringBuilder(stringBuilder.substring(i,s.length())).reverse().toString());
                break;
            }
            stringBuilder.replace(i,i+k,new StringBuilder(stringBuilder.substring(i,i+k)).reverse().toString());
        }

        return stringBuilder.toString();
    }
}
